//
//  BattleLayer.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-4-10.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "BattleScene.h"
#import "BattleGroupInfo.h"
#import "BattleGroupInfoMgr.h"
#import "Constants.h"
#import "Variable.h"
#import "SimpleAudioEngine.h"

@implementation BattleScene

@synthesize camera = _camera;

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	BattleScene *layer = [BattleScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];
    
	// return the scene
	return scene;
}


/**
	战斗场景主层的单件的引用
 */
static BattleScene * BattleLayerInstance;

+(BattleScene *) sharedLayer
{
    NSAssert(BattleLayerInstance != nil, @"MultiLayerScene not available!");
    
    return BattleLayerInstance;
}

/**
	背景层
	@returns 背景层得实例引用
 */
-(BattleBackGroundLayer*) backGroundLayer
{
    CCNode* layer = [self getChildByTag:LayerTagBackGround];
    NSAssert([layer isKindOfClass:[BattleBackGroundLayer class]], @"%@: not a BattleBackGroundLayer!",
             NSStringFromSelector(_cmd));
    return (BattleBackGroundLayer*)layer;
}


/**
	远陨石层
	@returns 远陨石层实例的引用
 */
-(BattleMeteoriteLayer*) farMeteoriteLayer
{
    CCNode* layer = [self getChildByTag:LayerTagFarMeteorite];
    NSAssert([layer isKindOfClass:[BattleMeteoriteLayer class]], @"%@: not a BattleMeteoriteLayer!",
             NSStringFromSelector(_cmd));
    return (BattleMeteoriteLayer*)layer;
}

/**
	近陨石层
	@returns 近陨石层实例的引用
 */
-(BattleMeteoriteLayer*) nearMeteoriteLayer
{
    CCNode* layer = [self getChildByTag:LayerTagNearMeteorite];
    NSAssert([layer isKindOfClass:[BattleMeteoriteLayer class]], @"%@: not a BattleMeteoriteLayer!",
             NSStringFromSelector(_cmd));
    return (BattleMeteoriteLayer*)layer;
}


/**
	主战斗层
	@returns 主战斗层实例的引用
 */
-(BattleMainLayer*) mainLayer
{
    CCNode* layer = [self getChildByTag:LayerTagMain];
    NSAssert([layer isKindOfClass:[BattleMainLayer class]], @"%@: not a BattleMainLayer!",
             NSStringFromSelector(_cmd));
    return (BattleMainLayer*)layer;
}

/**
	导弹层
	@returns 导弹层实例的引用
 */
-(BattleMissileLayer*) missileLayer
{
    CCNode* layer = [self getChildByTag:LayerTagMissile];
    NSAssert([layer isKindOfClass:[BattleMissileLayer class]], @"%@: not a BattleMissileLayer!",
             NSStringFromSelector(_cmd));
    return (BattleMissileLayer*)layer;
}

/**
	舰载机层
	@returns 舰载机层实例的引用
 */
-(BattleAirplaneLayer*) airplaneLayer
{
    CCNode* layer = [self getChildByTag:LayerTagAirplane];
    NSAssert([layer isKindOfClass:[BattleAirplaneLayer class]], @"%@: not a BattleAirplaneLayer!",
             NSStringFromSelector(_cmd));
    return (BattleAirplaneLayer*)layer;
}

/**
	UI层
	@returns UI层实例的引用
 */
-(BattleUILayer*) uiLayer
{
    CCNode* layer = [self getChildByTag:LayerTagUI];
    NSAssert([layer isKindOfClass:[BattleUILayer class]], @"%@: not a BattleUILayer!",
             NSStringFromSelector(_cmd));
    return (BattleUILayer*)layer;
}

/**
	输入层
	@returns 输入层得实例得引用
 */
-(BattleInputLayer*) inputLayer
{
    CCNode* layer = [self getChildByTag:LayerInputUI];
    NSAssert([layer isKindOfClass:[BattleInputLayer class]], @"%@: not a BattleInputLayer!",
             NSStringFromSelector(_cmd));
    return (BattleInputLayer*)layer;
}

-(id) init
{//缺宽和高
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) 
    {
        BattleLayerInstance = self;
        
        //缓冲图片文件，以后挪到别处
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"UIBattle_480_320.plist"];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Battle_480_320.plist"];
        
        //初始化战争视图类型,默认是微观视图
        extern BattleViewType viewType;
        viewType = BattleViewMicroscopic;
        
        //初始化屏幕的最大宽度和高度
        extern int screenMaxWidth;
        screenMaxWidth = 2160;
        extern int screenMaxHeight;
        screenMaxHeight = 2160;
        
        //初始化缩放比例
        extern float microscopicScale;
        extern float localScale;
        extern float macroscopicScale;
        extern float overallSituationScale;
        microscopicScale = 1.0;
        localScale = 0.44;
        macroscopicScale = 0.22;
        overallSituationScale = 0.15;
        
        //初始化层
        [self initLayer];
        
        //初始化镜头
        self.camera = [Camera cameraWithWidth:480 height:320 x:0 y:0];
        [self addChild: self.camera];
        
        //初始化战斗
        [self initBattle];
        
        //播放音乐
        [self schedule:@selector(checkBackgroundMusic:)]; 
    }
	return self;
}

/**
	初始化战斗中的层
 */
-(void) initLayer
{
    //背景层
    BattleBackGroundLayer* backGroundLayer = [BattleBackGroundLayer node];
    [self addChild:backGroundLayer z:1 tag:LayerTagBackGround]; 
    
    //远陨石层
    BattleMeteoriteLayer * farMeteoriteLayer = [BattleMeteoriteLayer meteoriteWithType:YES];
    [self addChild:farMeteoriteLayer z:2 tag:LayerTagFarMeteorite]; 
    
    //近陨石层
    BattleMeteoriteLayer* nearMeteoriteLayer = [BattleMeteoriteLayer meteoriteWithType:NO]; 
    [self addChild:nearMeteoriteLayer z:3 tag:LayerTagNearMeteorite];
    
    //主战斗层
    BattleMainLayer* mainLayer = [BattleMainLayer node]; 
    [self addChild:mainLayer z:4 tag:LayerTagMain];
    
    //导弹层
    BattleMissileLayer* missileLayer = [BattleMissileLayer node]; 
    [self addChild:missileLayer z:5 tag:LayerTagMissile];
    
    //舰载机层
    BattleAirplaneLayer* airplaneLayer = [BattleAirplaneLayer node]; 
    [self addChild:airplaneLayer z:6 tag:LayerTagAirplane];
    
    //UI层
    BattleUILayer* uiLayer = [BattleUILayer node]; 
    [self addChild:uiLayer z:7 tag:LayerTagUI];
    
    //输入层
    BattleInputLayer* inputLayer = [BattleInputLayer node]; 
    [self addChild:inputLayer z:8 tag:LayerInputUI];
}

/**
	初始化战斗相关,根据自己的舰队和当前关卡的战斗组进行初始化
 */
-(void) initBattle
{
    //初始化自己的舰队
    
    //初始化对方的舰队
    BattleGroupInfo* groupInfo = [[BattleGroupInfoMgr sharedManager] getInfoById:1001]; //根据groupId返回战斗组信息
    if(groupInfo != nil)
    {
        if(self.mainLayer != nil)
        {
            [self.mainLayer beginFight:nil enemiesTeam:groupInfo.shipIDs];
        }
    }
    else
    {
        CCLOG(@"战斗组不存在，groupId:%d", 1001);
    }
}

/**
	更新战争视图
 */
-(void) updateViewType:(BattleViewType) changeToType
{
    //更新远陨石层的视图等级
    [self.farMeteoriteLayer updateViewType:changeToType];
    
    //更新近陨石层的视图等级
    [self.nearMeteoriteLayer updateViewType:changeToType];
    
    //更新主战斗层的视图等级
    [self.mainLayer updateViewType:changeToType];
    
    //更新到导弹层的视图等级
    [self.missileLayer updateViewType:changeToType];
    
    //更新舰载机层的视图等级
    [self.airplaneLayer updateViewType:changeToType];
    
    //更新camera的视图等级
    [self.camera updateViewType:changeToType];
}

/**
	更新视角的移动，设置新的
 */
-(void) setViewpointCenter:(CGPoint) position
{
    //更新远陨石层的视图中点
    [self.farMeteoriteLayer setViewpointCenter:position];
    
    //更新近陨石层的视图中点
    [self.nearMeteoriteLayer setViewpointCenter:position];
    
    //更新主战斗层的视图中点
    [self.mainLayer setViewpointCenter:position];
    
    //更新到导弹层的视图中点
    [self.missileLayer setViewpointCenter:position];
    
    //更新舰载机层的视图中点
    [self.airplaneLayer setViewpointCenter:position];
}

-(void) checkBackgroundMusic:(ccTime)aTime
{
    [self unschedule:@selector(checkBackgroundMusic:)]; 
    
    [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"battle.mp3"];  
}

-(void) dealloc
{
    // BattleLayerInstance将在这里被释放,你必须将BattleLayerInstance变量设为nil 
    // 否则它将指向一个已经被释放的对象
    BattleLayerInstance = nil;
    
    if(self.camera != nil)
    {
        [self.camera release];
        self.camera = nil;
    }
    
    // 不要忘记调用"super dealloc" 
    [super dealloc];
}

@end
